package com.fengye.algorithms.leecode;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @Author fengye
 * @Date 2023/4/8 10:06
 **/
public class BracketValid20 {
    public boolean isValid(String s){
        if(s.isEmpty()) {
            return true;
        }
        Map<String, String> map = new HashMap<String,String>();
        map.put("(",")");
        map.put("[","]");
        map.put("{","}");

        Stack<String> stack = new Stack<>();
        boolean result = false;
        for(int i = 0; i < s.length();i++) {
            String a = s.charAt(i) + "";
            if(a.equals("(") || a.equals("[") || a.equals("{")) {
                stack.push(a);
                continue;
            }
            if(stack.isEmpty()) {
                return false;
            }
            String b = stack.peek();
            if(map.get(b).equals(a)) {
                stack.pop();
                continue;
            } else {
                return false;
            }
        }
        return stack.isEmpty();
    }
}
